//
//  HaodianViewController.m
//  HomeIn
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 ZZP. All rights reserved.
//

#import "HaodianViewController.h"
#import "HaodianModel.h"
#import "HaodianCell.h"
#import "HaodianDetailViewController.h"

@interface HaodianViewController () <UITableViewDelegate,UITableViewDataSource,push>

@property (nonatomic, strong) NSMutableArray *modelArray;

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, assign) NSInteger currentPage;//记录当前page
@property (nonatomic, assign) BOOL isUpLoading;//标记是否为上拉

@end

@implementation HaodianViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //视图将要出现的时候刷新
    [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.modelArray = [NSMutableArray array];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 113 - 30)  style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.tableView];
    
    //取消cell线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    self.currentPage = 0;//默认请求下来的数据为第0页
    self.isUpLoading = NO;//默认是下拉状态
    //[self getAFNetWorkingData:self.currentPage];//请求cell数据
    [self addHeaderRefresh];
    [self addFooterRefresh];
  
}

//下拉刷新
- (void)addHeaderRefresh
{
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        
        self.isUpLoading = NO;//标记为下拉
        NSLog(@"下拉");
        self.currentPage = 0;//刷新首页,只要下拉,就请求第0-20页数据
        //重新请求数据
        [self getAFNetWorkingData:self.currentPage];
    }];
    
    // 马上进入刷新状态
    [self.tableView.header beginRefreshing];
}

//上拉刷新
- (void)addFooterRefresh
{
    
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        
        self.isUpLoading = YES;//标记为上拉
        NSLog(@"上拉");
        self.currentPage++;//上拉一次,页码+1
        
        [self getAFNetWorkingData:self.currentPage];
    }];
    
}

- (void)getAFNetWorkingData:(NSInteger)page
{
    NSString *str = @"%20";//%20代表空格
    
    NSString *urlString = [NSString stringWithFormat:@"http://rm.app.diaoyuweng.com/json_shop.php?action=list&ver=0&page=%ld&tag=(null)&pageSize=10&appCookie=channel_AppStoreversion_4.3oid_0f607264fc6318a92b9e13c65db7cd3c%@package_com.aslibra.rm.jiaju%@platform_iphonever_9.1jb_0", self.currentPage, str, str];
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
       
        NSMutableDictionary *dictionary = responseObject;
        NSMutableArray *array = [dictionary objectForKey:@"data"];
        
        for (NSMutableDictionary *dic in array)
        {
            HaodianModel *haodianModel = [[HaodianModel alloc] init];
            [haodianModel setValuesForKeysWithDictionary:dic];
            
            haodianModel.picUrl = [[dic objectForKey:@"info"] objectForKey:@"picUrl"];
            haodianModel.fansCount = [[dic objectForKey:@"info"] objectForKey:@"fansCount"];
            
            [self.modelArray addObject:haodianModel];
        }
   
        //结束下拉上拉刷新
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
        
        //刷新
        [self.tableView reloadData];
        
    }
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             
             NSLog(@"error = %@",error);
             //本地缓存
         }];
    
}



//指定分区数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

//指定多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.modelArray.count;
    
}

//创建一个cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndentifier = @"cell";
    HaodianCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (cell == nil)
    {
        cell = [[HaodianCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;//设置cell点击效果 (没有置灰效果)
    //恢复cell线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    
    HaodianModel *haodianModel = [self.modelArray objectAtIndex:indexPath.row];
    cell.haodianModel = haodianModel;
    
    cell.MyDelegate = self;//协议代理
    
    return cell;
    
}

//设置行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return (HEIGHT - 113) / 2;
    
}

//点击cell触发方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HaodianDetailViewController *haodianDetialVC = [[HaodianDetailViewController alloc] init];
    HaodianModel *model = [self.modelArray objectAtIndex:indexPath.row];
    haodianDetialVC.model = model;//属性传值
    
    //push到详情的时候让tabBar(工具栏)消失
    haodianDetialVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:haodianDetialVC animated:YES];
}

//实现协议方法
- (void)pushToWeb:(HaodianModel *)model
{
    HaodianDetailViewController *haodianDetialVC = [[HaodianDetailViewController alloc] init];
    haodianDetialVC.model = model;//属性传值
    
    //push到详情的时候让tabBar(工具栏)消失
    haodianDetialVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:haodianDetialVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
